<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <p>欢迎来到叩丁狼积分商城</p>
      <ul class="navs">
        <li>
          <img :src="userInfo.headImg" alt="" />用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-show="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li
          class="current"
          @click="changeIsShowLoginModal(true)"
          v-show="!isLogined"
        >
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from "vuex";
import { WenXinLoginAPI } from "@/request/api";
export default {
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      aysncShowToast: "showToast/aysncShowToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  created() {
    console.log("topbar-created");
    this.$router.onReady(async () => {
      console.log("ready-code:", this.$route.query.code);
      let pathCode = this.$route.query.code;
      if (pathCode) {
        let res = await WenXinLoginAPI({
          code: pathCode,
        });
        console.log("*****res", res);
        if (res.code == 0) {
          this.aysncShowToast({
            isShow: true,
            msg: "登陆成功",
            type: "success",
          });
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          this.changeIsLogined(true);
          this.$router.push(this.$route.path);
        } else if (res.code == 400) {
          this.aysncShowToast({
            isShow: true,
            msg: "请重新扫码",
            type: "danger",
          });
          this.changeIsShowLoginModal(true);
        } else if (res.code == 407) {
          this.aysncShowToast({
            isShow: true,
            msg: "请绑定手机号",
            type: "warning",
          });
          this.changeIsShowLoginModal(true);
          localStorage.setItem("uuid", res.uuid);
          this.$router.push(this.$route.path);
          this.asyncChangeUserInfo();
        }
      } else {
        console.log("非扫码操作");
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token));
        if (token) {
          this.asyncChangeUserInfo();
        } else {
          this.initUserInfo();
        }
      }
    });
  },
  // watch: {
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       console.log("地址改变", newVal, oldVal);
  //       let token = localStorage.getItem("x-auth-token");
  //       this.changeIsLogined(Boolean(token));
  //     },
  //   },
  // },
};
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
}
.topbar {
  width: 100%;
  height: 40px;
  background: #242b39;
}
.topbarwrap {
  display: flex;
  justify-content: space-between;
  color: #fffefe;
  line-height: 40px;
  p {
    font-size: 14px;
    font-weight: 300;
  }
  .navs {
    li {
      float: left;
      margin-left: 10px;
      font-size: 14px;
      color: #fffefe;
      &.current {
        background-color: #0a328e;
        padding: 0 30px;
      }
    }

    img {
      width: 23px;
      height: 23px;
      border-radius: 50%;
      vertical-align: middle;
    }
    .cart-btn {
      width: 124px;
      height: 40px;
      background: #0a328e;
      text-align: center;
      img {
        width: 20px;
        height: 19px;
        vertical-align: middle;
      }
      span {
        margin: 0 8px;
      }
      strong {
        width: 22px;
        height: 22px;
        line-height: 22px;
        display: inline-block;
        border-radius: 50%;
        background-color: #fd604d;
      }
    }
  }
}
</style>